//
//  KDChooseViewController.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/12.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDChooseViewController.h"
#import "KDChooseTableViewCell.h"
@interface KDChooseViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView     *_tableView;
    NSArray         *_arr1;//按项目分,名字数组
    NSArray         *_arr2;//按方案分,名字数组
}

@end

@implementation KDChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setNaviTitle:@"筛选"];
    [self setNaviLeftButton];
    
    [self loadModel];
    
    _tableView = [[UITableView alloc] initWithFrame:(CGRect){0,0,kScreenWidth,kScreenHeight-64-35-16} style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
//    _tableView.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
    
    [self createSureButton];
}

- (void)createSureButton
{
    UIButton *btn1 = [self getButton];
    [btn1 setFrame:(CGRect){19,kScreenHeight-35-8-64,WIDTH(self.view)-38,35}];
    [btn1 setTitle:@"确 定" forState:UIControlStateNormal];
    [btn1 setBackgroundColor:kNavBlueColor];
    [btn1 setTag:101];
    [self.view addSubview:btn1];
}
- (UIButton *)getButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor clearColor]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
    return button;
}
- (void)buttonClick:(UIButton *)btn
{
    if (btn.tag == 101) {
        //
        KDChooseTableViewCell   *cell1 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:1]];
        
        KDChooseTableViewCell   *cell2 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:2]];
        
        KDChooseTableViewCell   *cell3 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:3]];
        KDChooseTableViewCell   *cell4 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:1 inSection:3]];
        KDChooseTableViewCell   *cell5 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:2 inSection:3]];
        
        KDChooseTableViewCell   *cell6 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:4]];
        KDChooseTableViewCell   *cell7 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:1 inSection:4]];
        KDChooseTableViewCell   *cell8 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:2 inSection:4]];
        KDChooseTableViewCell   *cell9 = [_tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:3 inSection:4]];
        if (!cell1.btn1.selected && !cell1.btn2.selected
            && cell2.textField.text.length == 0
            && !cell3.btn1.selected && !cell4.btn1.selected && !cell5.btn1.selected
            && !cell6.btn1.selected && !cell7.btn1.selected && !cell8.btn1.selected && !cell9.btn1.selected) {
            [self showAlert:@"请选择筛选条件"];
        }else{
            if ([self.chooseVCDeleagte respondsToSelector:@selector(chooseResultSex:age:disease:solution:)]) {
                NSString    *str1,*str2,*str3,*str4;
                if (cell1.btn1.selected) {
                    str1 = @"男";
                }else if (cell1.btn2.selected){
                    str1 = @"女";
                }else{
                    str1 = nil;
                }
                if (cell2.textField.text.length > 0) {
                    str2 = cell2.textField.text;
                }else{
                    str2 = nil;
                }
                if (cell3.btn1.selected) {
                    str3 = [_arr1 objectAtIndex:0];
                }else if (cell4.btn1.selected){
                    str3 = [_arr1 objectAtIndex:1];
                }else if (cell5.btn1.selected){
                    str3 = [_arr1 objectAtIndex:2];
                }else{
                    str3 = nil;
                }
                if (cell6.btn1.selected) {
                    str4 = [_arr2 objectAtIndex:0];
                }else if (cell7.btn1.selected){
                    str4 = [_arr2 objectAtIndex:1];
                }else if (cell8.btn1.selected){
                    str4 = [_arr2 objectAtIndex:2];
                }else if (cell9.btn1.selected){
                    str4 = [_arr2 objectAtIndex:3];
                }
                else{
                    str4 = nil;
                }
                
                [self.chooseVCDeleagte chooseResultSex:str1 age:str2 disease:str3 solution:str4];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }

    }
}
- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}
- (void)loadModel{
    self.headViewArray = [[NSMutableArray alloc]init ];
    NSArray *nameArr = [[NSArray alloc] initWithObjects:@"按时间(默认)",@"按性别",@"按年龄",@"按项目",@"按方案", nil];
    for(int i = 0;i< 5 ;i++)
    {
        KDChooseHeadCell* headview = [[KDChooseHeadCell alloc] init];
        headview.delegate = self;
        headview.section = i;
        headview.nameLabel.text = [nameArr objectAtIndex:i];
        if (i == 0) {
            headview.backImageView.frame = CGRectMake(100, 13, 15, 11);
            headview.backImageView.image = [UIImage imageNamed:@"right@2X.png"];
            headview.backBtn.selected = YES;
        }
        [self.headViewArray addObject:headview];
    }
    
    _arr1 = [NSArray arrayWithObjects:@"试管婴儿",@"妇科肿瘤",@"HPV", nil];
    _arr2 = [NSArray arrayWithObjects:@"试管婴儿--A方案",@"妇科肿瘤--A方案",@"妇科肿瘤--B方案",@"HPV--1方案", nil];

}
//headview delegate
-(void)selectedWith:(KDChooseHeadCell *)view
{
    _currentSection = view.section;

    if (view.open) {
//        for(int i = 0;i<[_headViewArray count];i++)
//        {
            KDChooseHeadCell *head = [_headViewArray objectAtIndex:_currentSection];
            head.open = NO;
            [head.backBtn setSelected:NO];
//        }
        [_tableView reloadData];
        return;
    }
    [self reset];
}
//界面重置
- (void)reset
{
    for(int i = 0;i<[_headViewArray count];i++)
    {
        KDChooseHeadCell *head = [_headViewArray objectAtIndex:i];
        
        if(head.section == _currentSection)
        {
            head.open = YES;
            [head.backBtn setSelected:NO];
            
        }
//        else {
//            [head.backBtn setSelected:NO];
//            head.open = NO;
//        }
        
    }
    [_tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = NO;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.headViewArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 35;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KDChooseHeadCell* headView = [self.headViewArray objectAtIndex:indexPath.section];
    
    return headView.open?35:0;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self.headViewArray objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    KDChooseHeadCell* headView = [self.headViewArray objectAtIndex:section];
    if (headView.open) {
        if (section == 0) {
            return 0;
        }else if (section == 1){
            return 1;
        }else if (section == 2){
            return 1;
        }else if (section == 3){
            return 3;
        }else{
            return 4;
        }
    }else{
        return 0;
    }
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
{
    return NO;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    [_tableView reloadData];
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CustomCellIdentifier = [NSString stringWithFormat:@"CellIdentifier%ld",(long)indexPath.row+indexPath.section*10];
    KDChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    
    if (cell ==nil) {
        cell = [[KDChooseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
        UIView  *line = [[UIView alloc] initWithFrame:(CGRect){0,34,kScreenWidth,1}];
        [line setBackgroundColor:[UIColor colorWithHexString:@"0xE0E0E0"]];
        [cell.contentView addSubview:line];
    }
    if (indexPath.section == 0) {
        //
    }else if (indexPath.section == 1){
        [cell reloadCell:indexPath name:nil];
    }else if (indexPath.section == 2){
        [cell reloadCell:indexPath name:nil];
    }else if (indexPath.section == 3){
        [cell reloadCell:indexPath name:[_arr1 objectAtIndex:indexPath.row]];
    }else{
        [cell reloadCell:indexPath name:[_arr2 objectAtIndex:indexPath.row]];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
